#include "nachiparseclass.h"

NachiParseClass::NachiParseClass()
{

}

int NachiParseClass::parseToNulanguage(QString &incomeProgram, QStringList &cmdStrListOut,
                                       QVector<PointPro> &pointListOut, QStringList &messageOut)
{

    qDebug()<<"nachi incomeProgram"<<incomeProgram;

    Program outputProgram;
    int currentPointIndex=0;
    QStringList textLineList = incomeProgram.split("\n"); // 需要写入的模板数据
    QVector<double> currentPose;
    currentPose.resize(6);
    // 遍历所有行
    QRegExp rx(".*MOVEX.*");
    QRegExp rx_pValue;
    Line tmpLine;
    PointPro tmpPoint;
    for(int i=0;i<textLineList.size();i++)
    {
        qDebug()<<"thisLine:"<<textLineList[i];
        bool isMatch=rx.exactMatch(textLineList[i]);
        if(false==isMatch )
        {
            qDebug()<<"unknow非MOVEX 指令"<<textLineList[i];
            continue;
        }

        rx_pValue.setPattern(".*MOVEX\\s*A=\\d*,AC=\\d*,SM=\\d*,M1X,(P*L*),\\((.*)\\)\\S*.*");
        isMatch=rx_pValue.exactMatch(textLineList[i]);
        if(false==isMatch )
        {
            qDebug()<<"unknowMOVEX false==isMatch "<<textLineList[i];
            continue;
        }
        tmpLine.type=MOTION;

        QString tmpMoveTypeStr=rx_pValue.cap(1);
        if("P"==tmpMoveTypeStr)
        {
            tmpLine.motion.motionFormat=E_MOTION_J;
        }
        else if("L"==tmpMoveTypeStr)
        {
            tmpLine.motion.motionFormat=E_MOTION_L;
        }
        else
        {
            qDebug()<<"unknowMOVEX 3 false==isMatch tmpMoveTypeStr"<<tmpMoveTypeStr;
            return 0;
        }

        //p
        QStringList axisValuesStr = rx_pValue.cap(2).split(",");
        for(int n=0;n<axisValuesStr.size();n++)
        {
            if(n<currentPose.size())
            {
                currentPose[n]=axisValuesStr[n].toDouble();
            }
        }

        ++currentPointIndex;
        tmpLine.motion.p=currentPointIndex;
        tmpLine.motion.cnt=100;
        tmpLine.motion.rate=100;
        outputProgram.append(tmpLine);
        tmpPoint.jointFlag=1;
        tmpPoint.ufValue=defaultUserCoordNumber;
        tmpPoint.utValue=defaultToolCoordNumber;
        tmpPoint.positionValue=currentPose.toStdVector();
        tmpPoint.pointNum=tmpLine.motion.p;
        pointListOut.append(tmpPoint);


    }

    transProgramStructToString(outputProgram,cmdStrListOut );
    return 1;
}
